package com.cll.prototype.mongodb.springboot.dao.impl;

import com.cll.prototype.mongodb.springboot.bean.ResumePreview;
import com.cll.prototype.mongodb.springboot.dao.ResumePreviewDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 描述信息:
 *
 * @author CLL
 * @version 1.0
 * @date 2020/11/29 14:52
 */
@Repository
public class ResumePreviewDaoImpl implements ResumePreviewDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据实体类名称生成集合名称
     * @param resumePreview 简历概要
     */
    @Override
    public void insertResumePreview(ResumePreview resumePreview) {
        mongoTemplate.insert(resumePreview, "resume_preview");
    }

    @Override
    public ResumePreview findByName(String name) {
        Query queryParam = new Query();
        queryParam.addCriteria(Criteria.where("name").is(name));
        List<ResumePreview> resumePreviewList = mongoTemplate.find(queryParam, ResumePreview.class, "resume_preview");
        return resumePreviewList.isEmpty() ? null : resumePreviewList.get(0);
    }

    @Override
    public List<ResumePreview> listByName(String name) {
        Query queryParam = new Query();
        queryParam.addCriteria(Criteria.where("name").is(name));
        return mongoTemplate.find(queryParam, ResumePreview.class, "resume_preview");
    }

    @Override
    public ResumePreview findByName(String name, Double expectSalary) {
        Query queryParam = new Query();
        queryParam.addCriteria(Criteria.where("name").is(name).andOperator(Criteria.where("expectSalary").is(expectSalary)));
        List<ResumePreview> resumePreviewList = mongoTemplate.find(queryParam, ResumePreview.class, "resume_preview");
        return resumePreviewList.isEmpty() ? null : resumePreviewList.get(0);
    }
}
